package com.food.controller;

import com.food.pojo.FoodsDto;
import com.food.result.Result;
import com.food.service.FoodsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author roudoukouya
 * @version 1.0
 * @description 编写用户历史记录接口,
 * 用户点击过某个美食, 就记录下来, isCollect = 0 表示看过
 * 点击收藏的时候, isCollect = 1
 * @date 2023/4/5
 */
@Api(tags = "收藏美食")
@CrossOrigin("*")
@RestController
@RequestMapping("/api/browse")
public class FavoriteController {
    @Autowired
    private FoodsUserService foodsUserService;

    @ApiOperation(value = "收藏美食", notes = "isFavorite: 1 表示收藏 , 0 表示取消收藏")
    @GetMapping(value = {"/favorite/{foodId}/{userId}/{isFavorite}"})
    public Result favoriteFoods(@PathVariable(value = "foodId") Integer foodId,
                                @PathVariable(value = "userId") Integer userId,
                                @PathVariable(value = "isFavorite") Integer isFavorite) {
        return Result.ok(foodsUserService.updateIsCollectByFoodId(userId, foodId, isFavorite));
    }

    @ApiOperation("添加到历史记录")
    @GetMapping("/add/history/record/{foodId}/{userId}")
    public Result addHistoryRecord(@PathVariable(value = "foodId") Integer foodId,
                                   @PathVariable(value = "userId") Integer userId
    ) {
        // 先查历史
        int record = foodsUserService.getHistoryRecord(userId, foodId);
        if (record <= 0) {
            return Result.ok(foodsUserService.insertHistoryRecord(userId, foodId));
        } else {
            return Result.ok();
        }
    }

    @ApiOperation("查询是否已经被收藏过了")
    @GetMapping("/collect/{foodId}/{userId}")
    public Result getCollectByUserIdAndFoodId(@PathVariable(value = "foodId") Integer foodId,
                                              @PathVariable(value = "userId") Integer userId
    ) {
        return Result.ok(foodsUserService.getIsCollectByFoodIdAndUserId(foodId, userId));
    }

    @ApiOperation(value = "查询收藏的美食", notes = "只查询userId下的所有的收藏, 并不会去查询别人的收藏")
    @GetMapping("/collect/{userId}")
    public Result getAllCollectByUserIdAndFoodId(
            @PathVariable(value = "userId") Integer userId
    ) {
        return Result.ok(foodsUserService.selectAllCollectRecord(userId));
    }

    @ApiOperation(value = "查询历史记录", notes = "只查询userId下的所有的历史记录, 并不会去查询别人的历史记录")
    @GetMapping("/history/{userId}")
    public Result getHistoryByUserId(
            @PathVariable(value = "userId") Integer userId
    ) {
        return Result.ok(foodsUserService.getHistoryByUserId(userId));
    }


}
